<template>
  <div class="dashboard-container">
    <div class="app-container">
      <!-- 表单组件 -->
      <el-card>
        <!-- 表头 -->
        <el-row type="flex" justify="space-between" class="top-box">
          <el-col>
            <el-button type="primary" @click="addPages">添加页面</el-button>
          </el-col>
          <el-row type="flex" justify="start" class="top-right">
            <el-input v-model="title_contains" placeholder="请输入需要搜索的标题" />
            <el-button icon="el-icon-search" @click="search" />
          </el-row>
        </el-row>
        <hr style="border:1px solid #f5f7fa">
        <!-- 表格内容 -->
        <el-table :data="tableData" border style="width: 100%" class="top">
          <el-table-column label="标题" prop="title" />
          <el-table-column label="内容" prop="content">
            <template v-slot="{row}">
              <div v-html="row.content" />
            </template>
          </el-table-column>

          <el-table-column label="操作">
            <template v-slot="{row}">
              <el-button size="mini" type="primary" @click="editPages(row.id)">编辑</el-button>
              <el-button size="mini" type="danger" @click="del(row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-row type="flex" justify="center" align="middle" style="height: 60px">
          <el-pagination
            layout="prev, pager, next"
            :page-size="pageSize"
            :total="total"
            @current-change="pageChange"
          />
        </el-row>
      </el-card>

      <!-- 弹窗组件 -->
      <el-dialog

        :title="formData.id?'编辑页面':'添加页面'"
        :visible="dialogVisible"
        width="40%"
        @close="closeFn"
      >
        <!-- form表单 -->
        <el-form ref="ruleForm" label-width="80px" :model="formData" :rules="rules">
          <el-form-item label="标题" prop="title">
            <el-input v-model="formData.title" />
          </el-form-item>
          <el-form-item label="内容" class="body" prop="content">
            <!-- <el-input v-model="formData.region" /> -->
            <quill-editor ref="text" v-model="formData.content" class="editor" :options="editorOption" />
          </el-form-item>

        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-row type="flex" justify="center">
            <el-button @click="closeFn">取 消</el-button>
            <el-button type="primary" @click="submit">确 定</el-button>
          </el-row>
        </span>
      </el-dialog>

    </div>
  </div>
</template>
<script>
import { delPages, editPages, getPagesById, getPagesList, increasePages } from '@/api/pages'

export default {
  data() {
    return {
      tableData: [],
      dialogVisible: false,
      page: 0,
      total: 0,
      pageSize: 2,
      title_contains: '',
      formData: {
        title: '',
        content: ''
      },
      editorOption: {},
      rules: {
        title: [
          { required: true, message: '请输入标题', trigger: 'blur' },
          { min: 1, max: 10, message: '长度在 1 到 10 个字符', trigger: 'blur' }
        ],
        content: [
          { required: true, message: '请输入内容', trigger: 'blur' },
          { min: 1, max: 300, message: '长度在 1 到 300 个字符', trigger: 'blur' }

        ]
      }
    }
  },
  async created() {
    this.getPages()
    // const res = await getPagesList()
    // this.total = res.data.length
    this.getPagesNumber()
  },
  methods: {
    async getPagesNumber() {
      const res = await getPagesList()
      // console.log('129', res)
      this.total = res.data.length
    },
    // 获取页面列表
    async getPages(val) {
      if (val) {
        const result = await getPagesList({
          title_contains: val
        })
        this.tableData = result.data
        this.total = result.data.length
      } else {
        const res = await getPagesList({
          _limit: this.pageSize,
          _start: this.page
        })
        console.log('83', res)
        this.tableData = res.data
      }
    },
    addPages() {
      this.dialogVisible = true
    },
    //  确认按钮
    submit() {
      this.$refs.ruleForm.validate(async(valid) => {
        if (valid) {
          if (this.formData.id) {
            await editPages(this.formData)
          } else {
            await increasePages(this.formData)
          }
          console.log('132', this.formData)
          // 关闭弹窗
          // this.dialogVisible = false
          this.closeFn()
          // 重新渲染
          this.getPages()
          this.getPagesNumber()
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    // 搜索功能
    async search(value) {
      console.log('144', value)
      // this.name_contains = value
      await this.getPages(this.title_contains)
    },
    // 切换分页
    pageChange(page) {
      this.page = (page - 1) * this.pageSize
      this.getPages()
      this.getPagesNumber()
    },
    // 编辑按钮
    async editPages(id) {
      const res = await getPagesById(id)
      console.log('147', res)
      this.dialogVisible = true
      this.formData = res.data
    },
    // 删除按钮
    del(id) {
      // console.log(97, id)
      this.$confirm('确定删除该页面吗？', '提示', {
        type: 'warning'
      }).then(async() => {
        // console.log('确定')
        // 删除
        await delPages(id)
        this.getPages()
        this.getPagesNumber()

        // 更新列表，当删除最后一条数据的时候，需要请求上一页的数据
        if (this.tableData.length === 1 && this.page !== 0) {
          this.page = this.page - this.pageSize
        }
        // 更新列表
        this.getPages()
      }).catch(err => {
        console.log('取消：', err)
      })
    },
    closeFn() {
      this.dialogVisible = false
      this.$refs.ruleForm.resetFields()
    }
  }
}
</script>
<style scoped lang="scss">
::v-deep .el-input__inner {
  width: 400px;
}
.biankuan {
  border-top: #dcdfe6;
}
.top{
    margin-top: 20px;
}
.body{
  height: 400px;
  .editor{
    height: 300px;
  }
}
</style>
